package com.guanjia.service.impl;

import com.guanjia.entity.CreditInformation;
import com.guanjia.entity.Repayment;
import com.guanjia.entity.User;
import com.guanjia.entity.vo.BorrowInfoVo;
import com.guanjia.mapper.BorrowMapper;
import com.guanjia.mapper.UserMapper;
import com.guanjia.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BorrowServiceImpl implements BorrowService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BorrowMapper borrowMapper;


    /**
     * borrowInfoVo  获取借贷页面传递的参数 重新包装
     *
     * @param borrowInfoVo
     * @return
     */
    public String firstBorrow(BorrowInfoVo borrowInfoVo){
        UserDetails userDetails=null;
      try {
         userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
      }
      catch (Exception e){
          return "0";
      }
      String username= userDetails.getUsername();
         System.out.println(username);
        User u=new User();

        u.setTell(username);
        Integer money=Integer.parseInt(borrowInfoVo.getBorrowmoney());
        Double remoney=Double.valueOf(borrowInfoVo.getRateofreturn());
        Integer time=Integer.parseInt(borrowInfoVo.getBorrowtime());
        Double reMoney=money*(1+remoney/100)/time;
        Double nowMoney=(double) Math.round(reMoney * 100) / 100;
      User user =  userMapper.findUserBytell(username);
        CreditInformation creditInformation=new CreditInformation();
        creditInformation.setUserId(user.getId());
        creditInformation.setProjectname(borrowInfoVo.getProjectname());
        creditInformation.setCity(borrowInfoVo.getCity());
        creditInformation.setBorrowmoney(Integer.parseInt(borrowInfoVo.getBorrowmoney()));
        creditInformation.setProvince(borrowInfoVo.getProvince());
        creditInformation.setBorrowtime(borrowInfoVo.getBorrowtime());
        creditInformation.setProjectabstract(borrowInfoVo.getProjectabstract());
        creditInformation.setRateofreturn(Double.valueOf(borrowInfoVo.getRateofreturn()));
        creditInformation.setMonthlyReturn(nowMoney);


        //重新插入到 creditInformation表
        borrowMapper.insert(creditInformation);


         return creditInformation.getId()+"";

    }

    @Override
    public List<CreditInformation> newList() {
        List<CreditInformation> list = borrowMapper.newList();
        return list;
    }

    @Override
    public Integer findcountById(Integer id) {
        return borrowMapper.findcountById(id);
    }

    @Override
    public Integer findacessById(Integer id) {
        return borrowMapper.findacessById(id);
    }

    @Override
    public Double sumBorrowMoney(Integer id) {
        return borrowMapper.sumBorrowMoney(id);
    }

    @Override
    public List<Repayment> findRepay(Integer id) {
        List<Repayment> list = borrowMapper.findRepay(id);
        return list;
    }

    @Override
    public Integer repaycount(Integer id) {
        return borrowMapper.repaycount(id);
    }

}
